package com.jeeplus.modules.guide.service;

import com.alibaba.fastjson.JSON;
import com.jeeplus.common.utils.RedisUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.database.datasource.annotation.DS;
import com.jeeplus.modules.guide.mapper.HisQueueMapper;
import com.jeeplus.modules.guide.model.constant.GuideConstant;
import com.jeeplus.modules.guide.model.domain.AvgPeople;
import com.jeeplus.modules.guide.model.domain.HisQueue;
import com.jeeplus.modules.guide.model.domain.MatterArea;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: HisQueueService
 * Function:  取票历史
 * Date:      2021/6/11 18:35
 *
 * @author hanKongbin
 * version    V1.0
 */
@DS("queuing")
@RequiredArgsConstructor
@Service
@Slf4j
public class HisQueueService {

    private final HisQueueMapper hisQueueMapper;

    private final RedisUtils redisUtils;

    @Value("${guide.cache.expiretime.hisServeTime}")
    private long hisServeTime;

    /**
     * 获取历史排队时间
     *
     * @param busId
     * @param sysNo
     * @return
     */
    public HisQueue getHistoryQueueTime(String busId, String sysNo) {
        if (StringUtils.isEmpty(sysNo) || StringUtils.isEmpty(busId)) {
            return null;
        }
        HisQueue data = new HisQueue();

        String key = GuideConstant.GUIDE_BUSINESS_HISAVGTIME + sysNo + busId;
        try {
            if (redisUtils.hasKey(key)) {
                return JSON.parseObject(redisUtils.get(key).toString(), HisQueue.class);
            }

            data = hisQueueMapper.avgTimeByBusinessId(busId, sysNo);

            if (null != data) {
                redisUtils.set(GuideConstant.GUIDE_BUSINESS_HISAVGTIME + busId, JSON.toJSONString(data), hisServeTime);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("获取业务历史等待时间失败，业务id->{}", busId);
        }
        return data;
    }

    public List<AvgPeople> avgPeopleByBusinessId(String busId, String sysNo, String startTime, String endTime) {
        if(StringUtils.isEmpty(busId) || StringUtils.isEmpty(sysNo) || StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)){
            return null;
        }
        return hisQueueMapper.avgPeopleByBusinessId(busId, sysNo, startTime, endTime);
    }

    public List<AvgPeople> avgPeopleAll(String type,String startTime, String endTime) {
        if("1".equals(type)){
            return hisQueueMapper.avgPeopleAllNumber(startTime, endTime);
        }else if("2".equals(type)){
            return hisQueueMapper.avgPeopleAllYY(startTime, endTime);
        }else if("3".equals(type)){
            return hisQueueMapper.avgPeopleAllPJ(startTime, endTime);
        }else{
            return hisQueueMapper.avgPeopleAllPJErr(startTime, endTime);
        }
    }

    public List<MatterArea> queryAreaById(List<String> id) {
        if (CollectionUtils.isEmpty(id)) {
            return new ArrayList<>();
        }
        return hisQueueMapper.queryAreaById(id);
    }

    public String getAddressValue(String areaId){
        return hisQueueMapper.getAddressValue(areaId);
    }
    public String getTypeNameValue(String businessId){
        return hisQueueMapper.getTypeNameValue(businessId);
    }
}
